<?php
require('../include/init.inc.php');
$method = $company_name = $copyright = $title = $keyword = $desc = $countdown = $tj_code = '';
extract($_REQUEST, EXTR_IF_EXISTS);

if (Common::isPost()) {
    $update_data = array();

    if (strlen($_REQUEST['company_name']) > 30) {
        exit(json_encode(array("code" => -1001, "msg" => ErrorMessage::STR_LENGTH_IELLEGE)));
//        Common::exitWithError(ErrorMessage::STR_LENGTH_IELLEGE, "snap/sys_config.php");
    }
    if (!empty($company_name)) {
        $update_data['footer_company'] = $_REQUEST['company_name'];
    }
    if (!empty($copyright)) {
        $update_data['footer_copyright'] = $_REQUEST['copyright'];
    }
    if (!empty($title)) {
        $update_data['title'] = $_REQUEST['title'];
    }
    if (!empty($keyword)) {
        $update_data['kewords'] = $_REQUEST['keyword'];
    }
    if (!empty($desc)) {
        $update_data['description'] = $_REQUEST['desc'];
    }
    if (!empty($tj_code)) {
        $update_data['tj_code'] = $_REQUEST['tj_code'];
    }


    //先检查没有配置就是插入


    $ret = AdminSysConfig::getInfo();
    if ($ret) {
        $result = AdminSysConfig::replace($update_data, $ret['id']);
    } else {
        $data = array("footer_company" => $_REQUEST['company_name'], "title" => $_REQUEST['title'], "kewords" => $_REQUEST['keyword'], "description" => $_REQUEST['desc'], "tongji_code" => $_REQUEST['tj_code'], "footer_copyright" => $_REQUEST['copyright']);
        $result = AdminSysConfig::add($data);
    }


    if ($result) {
        SysLog::addLog(UserSession::getUserName(), 'Replace', 'SysConfig', $title, json_encode($update_data));
//        Common::exitWithSuccess('更新完成', 'snap/sys_config.php');
        exit(json_encode(array("code" => 0, "msg" => ErrorMessage::STR_LENGTH_IELLEGE)));
    } else {
//        OSAdmin::alert("error");
        exit(json_encode(array("code" => -1002, "msg" => "失败")));
    }
}

Template::display('snap/sys_config.tpl');